package org.hs.im.forensicj.view;

import java.awt.Component;
import java.awt.image.RenderedImage;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import org.hs.im.forensicj.Application;
import org.hs.im.forensicj.ApplicationWindow;
import org.hs.im.forensicj.view.action.DenoiseMeanAction;
import org.hs.im.forensicj.view.action.DenoiseMedianAction;
import org.hs.im.forensicj.view.action.ImageOpenAction;
import org.hs.im.forensicj.view.imageviewer.ImageDockable;

import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CLocation;
import bibliothek.gui.dock.common.CWorkingArea;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.SingleCDockable;
import bibliothek.gui.dock.common.event.CFocusListener;
import bibliothek.gui.dock.common.intern.CDockable;
import bibliothek.gui.dock.common.theme.ThemeMap;

public class DefaultApplicationWindow implements ApplicationWindow {

	private JFrame frame;
	private Application application;
	private CControl control;
	private SingleCDockable projectFilesDockable;
	private SingleCDockable operationsListDockable;
	private SingleCDockable operationsLogDockable;
	private CWorkingArea imagesWorkingArea;
	protected ImageDockable activeImageDockable;

	public DefaultApplicationWindow() {

		createGUI();

	}

	private void createGUI() {

		frame = new JFrame("ForensicJ beta v 0.0.1 by HS");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setJMenuBar(createJMenuBar());

		control = new CControl(frame);
		control.setTheme(ThemeMap.KEY_FLAT_THEME);

		frame.add(control.getContentArea());

		projectFilesDockable = createProjectFilesDockable();
		operationsListDockable = createOperationsListDockable();
		operationsLogDockable = createOperationsLogDockable();
		imagesWorkingArea = control.createWorkingArea( "images working area");
		

		control.addMultipleDockableFactory( "images dockable", ImageDockable.FACTORY);
		
		control.add(projectFilesDockable);
		control.add(operationsListDockable);
		control.add(operationsLogDockable);

		projectFilesDockable.setLocation(CLocation.base().normalWest(0.2).north(0.5));
		operationsListDockable.setLocation(CLocation.base().normalWest(0.2).south(0.5));
		imagesWorkingArea.setLocation(CLocation.base().normal());
		operationsLogDockable.setLocation(CLocation.base().normalEast(0.7).south(0.2));
		
		
		projectFilesDockable.setVisible(true);
		operationsListDockable.setVisible(true);
		operationsLogDockable.setVisible(true);
		imagesWorkingArea.setVisible(true);
		


		frame.setBounds(20, 20, 400, 400);

	}

	private JMenuBar createJMenuBar() {
		JMenuBar mbr = new JMenuBar();
		JMenu mnuFile = new JMenu("File");
		mbr.add(mnuFile);
		mnuFile.add( new ImageOpenAction() );
		
		JMenu mnuDenoise = new JMenu("Denoise");
		mbr.add(mnuDenoise);
		mnuDenoise.add( new DenoiseMeanAction() );
		mnuDenoise.add( new DenoiseMedianAction() );

		
		return mbr;
	}

	private SingleCDockable createProjectFilesDockable() {
		JPanel pnl = new JPanel();
		String title = "Project Files";
		return new DefaultSingleCDockable(title, title, pnl);
	}

	private SingleCDockable createOperationsListDockable() {
		JPanel pnl = new JPanel();
		String title = "Operations";
		return new DefaultSingleCDockable(title, title, pnl);
	}

	private SingleCDockable createOperationsLogDockable() {
		JPanel pnl = new JPanel();
		String title = "Log";
		return new DefaultSingleCDockable(title, title, pnl);
	}


	@Override
	public void setApplication(Application application) {
		this.application = application;
	}

	@Override
	public Component getComponent() {
		return frame;
	}

	@Override
	public void openImage(String name, RenderedImage image) {
		final ImageDockable imageDockable = new ImageDockable( name, image);

		imageDockable.addFocusListener( new CFocusListener() {
			
			@Override
			public void focusLost(CDockable dockable) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void focusGained(CDockable dockable) {
				activeImageDockable = imageDockable;
			}
		});

		
		control.add(imageDockable);
		
		//TODO do something using WorkingArea 
		imageDockable.setWorkingArea(imagesWorkingArea);
		imageDockable.setVisible(true);
		

	}

	@Override
	public void setVisible(boolean b) {
		frame.setVisible(b);
	}

	@Override
	public ImageViewer getActiveImageViewer() {
		return (activeImageDockable == null) ? null : activeImageDockable.getImageViewer();
	}

}
